package com.qidian.hw.servlet;

import com.qidian.hw.pojo.SalaryInfos;
import com.qidian.hw.service.SalaryService;
import com.qidian.hw.service.impl.SalaryServiceImpl;
import com.qidian.hw.tools.Const;
import com.qidian.hw.tools.JSONTools;
import com.qidian.hw.tools.dto.Pager;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

/**
 * @author ������
 * @company �����
 * <p>
 * 2022��8��20�� ����2:17:26
 */
public class SalaryServlet extends HttpServlet {
    private SalaryService salaryService = new SalaryServiceImpl ();

    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if (req.getMethod ().equalsIgnoreCase ("POST")) {
            req.setCharacterEncoding ("utf-8");
        }
        String op = req.getParameter ("op");
        switch (op) {
            // ��������
            case "save":
                save (req, resp);
                break;
            // ��������
            case "input":
                input (req, resp);
                break;
            // �޸�����
            case "update":
                update (req, resp);
                break;
            // ��ʾ�����б�
            case "list":
                list (req, resp);
                break;
            // ɾ������
            case "delete":
                delete (req, resp);
                break;
            case "queryBypage":
                queryBypage (req, resp);
                break;
            case "deletes":
                deletes (req, resp);
                break;
            default:
                super.service (req, resp);
        }
    }

    private void deletes(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String result = "0";
        String[] ids = req.getParameterValues ("salaryId");
        if (ids != null && ids.length > 0) {
            result = salaryService.deletes (ids) + "";
        }
        resp.setContentType (Const.RES_PLAIN);
        resp.getWriter ().write (result);
    }

    private void queryBypage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int page = 1;
        try {
            page = Integer.parseInt (req.getParameter ("page"));
        } catch (NumberFormatException e) {
        }

        int size = 10;
        try {
            size = Integer.parseInt (req.getParameter ("size"));
        } catch (NumberFormatException e) {
        }
        int id = 0;
        try {
            id = Integer.parseInt (req.getParameter ("salaryId"));
        } catch (NumberFormatException e) { }
        String title = req.getParameter ("title");
        Pager pager = salaryService.queryByPage (page, size, title, id);
        resp.setContentType (Const.RES_JSON);
        resp.getWriter ().write (JSONTools.object2json (pager));
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Long salaryId = 0L;
        String result = "0";
        try {
            salaryId = Long.parseLong (req.getParameter ("salaryId"));
        } catch (NumberFormatException e) {
            e.printStackTrace ();
        }
        if (salaryId != 0) {
            result = salaryService.delete (salaryId) + "";
        }
        resp.setContentType (Const.RES_PLAIN);
        resp.getWriter ().write (result);
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ArrayList<SalaryInfos> salarys = salaryService.queryAll ();
        String jsons = JSONTools.object2json (salarys);
        resp.setContentType (Const.RES_JSON);
        resp.getWriter ().write (jsons);
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Long salaryId = 0L;
        try {
            salaryId = Long.parseLong (req.getParameter ("salaryId"));
        } catch (NumberFormatException e) { }
        String salaryDesc = req.getParameter ("salaryDesc");
        String position = req.getParameter ("position");
        SalaryInfos salary = new SalaryInfos ();
        salary.setSalaryDesc (salaryDesc);
        salary.setPosition (position);
        salary.setSalaryId (salaryId);
        int result = salaryService.update (salary);
        resp.setContentType (Const.RES_HTML);
        if (result == 1) {
            resp.getWriter ().write ("修改成功");
        } else resp.getWriter ().write ("修改失败");
    }

    private void input(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Long salaryId = Long.parseLong (req.getParameter ("salaryId"));
        SalaryInfos salary = salaryService.queryById (salaryId);
        String json = JSONTools.object2json (salary);
        System.out.println (json);
        resp.setContentType (Const.RES_JSON);
        resp.getWriter ().write (json);
    }

    private void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String salaryDesc = req.getParameter ("salaryDesc");
        String position = req.getParameter ("position");
        SalaryInfos salary = new SalaryInfos ();
        salary.setSalaryDesc (salaryDesc);
        salary.setPosition (position);
        int result = salaryService.save (salary);
        resp.setContentType (Const.RES_HTML);
        if (result == 1) {
            resp.getWriter ().write ("添加成功");
        } else resp.getWriter ().write ("添加失败");
    }
}
